/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package poker.game;

import java.util.ArrayList;

import java.util.Iterator;
import java.util.List;


/**
 *
 * @author Benjamin L. Brodie <blbrodie@gmail.com>
 */
public class Player {
    private PocketHand pocket = null;
    private int currentChips = 0;
    private int totalChipsWon = 0;
    private List<Double> weights;
    
    TexasMinimaxFunction evaluation;
    
    Minimax minimax;
    
    public int getCurrentChips(){
        return currentChips;
    }
    
    public int getTotalChipsWon(){
        return totalChipsWon;
    }
    
    public PocketHand getPocket(){
        return pocket;
    }
    
    public List<Double> getWeights(){
        return weights;
    }
    
    /**
     * update chips to amount. This does not add the amount.
     * @param updatedChipAmount 
     */
    public void updateCurrentChips(int updatedChipAmount){
        currentChips = updatedChipAmount;
    }
    
    
    public Player(List<Double> weights){
        this.weights = weights;
        this.evaluation = new TexasMinimaxFunction(weights);
        this.minimax = new Minimax(evaluation);
    }
    
    public void setPocketCards(Card c1, Card c2){

        pocket = new PocketHand(c1, c2);
    }
    
    /**
     * Add the value to the amount of chips won or lost.
     * 
     * @param value 
     */
    public void addToTotalChipsWon(int value){
        totalChipsWon += value;
    }
    
    public Move nextMove(int level, int playerChips, int opponentChips, boolean preFlop, TexasHand flop){
        
        if (pocket == null){
            throw new IllegalStateException("pocket is null!");
        }
        
        List<Integer> parameterList = convertToList(pocket, flop, totalChipsWon);
        assert parameterList.size() == 17: "Parameter list is wrong size";
        
        return minimax.nextMove(level, playerChips, opponentChips, preFlop, 
                parameterList);
        
    }
    
    /**
     * Combines the evaluation of the pocket and flop 
     * @param pocket
     * @param flop
     * @return 
     */
    private List<Integer> convertToList(PocketHand pocket, TexasHand flop, int totalChips){
        List<Card> pocketPlusFlop = new ArrayList<Card>();
        pocketPlusFlop.addAll(pocket.getHand());
        pocketPlusFlop.addAll(flop.getHand());
        
        TexasHand fullHand = new TexasHand(pocketPlusFlop);
        
        List<Integer> fullList = new ArrayList<Integer>();
        fullList.addAll(pocket.getEval()); //add the pocket eval - 4 spaces
        fullList.addAll(flop.getEval()); //add the flop eval - 4 spaces
        fullList.addAll(fullHand.getEval()); //add the full hand eval - 4 spaces
        
        
        //get difference between full hand and flop
        Iterator<Integer> flopIter = flop.getEval().iterator();
        Iterator<Integer> fullHandIter = fullHand.getEval().iterator();
        List<Integer> difference = new ArrayList<Integer>();
        while (flopIter.hasNext()){
            difference.add(fullHandIter.next() - flopIter.next());
        }
        
        fullList.addAll(difference); //add the differences eval - 4 spaces
        fullList.add(totalChips); //add the chips - 1 space
        
        
        
        
        return fullList;
    }
}
